package com.util;

import java.util.Random;

public class RandomPasswdCreater {
	/**
	 * @param 随机密码生成器
	 *            author goldlion 2007.05.11
	 */

	// 随机数产生
	public static final Random V_RandomNumber = new Random();

	// 数字0~9
	public static final StringBuffer V_Number;

	// 英文小写
	public static final StringBuffer V_CharLowCase;

	// 英文大写
	public static final StringBuffer V_CharUpCase;

	// 英文字母和数字
	public static final StringBuffer V_Char;
	
	// 英文小写和数字
	public static final StringBuffer V_CharLow;

	public RandomPasswdCreater() {
	}

	/**
	 * @param stringbuffer
	 * V_Number 数字0~9,
	 * V_CharLowCase 英文小写,
	 * V_CharUpCase 英文大写,
	 * V_Char 英文字母和数字,
	 * V_CharLow 英文小写和数字
	 * @param i 密码生成位数
	 * @return
	 */
	public static final String Produce(StringBuffer stringbuffer, int i) {
		if (stringbuffer.length() == 0) {
			return "参数不足!";
		}
		StringBuffer stringbuffer1 = new StringBuffer();
		while (i-- > 0) {
			int j = V_RandomNumber.nextInt(stringbuffer.length());
			stringbuffer1.append(stringbuffer.charAt(j));
		}
		return stringbuffer1.toString();
	}

	static {
		V_Number = new StringBuffer();
		V_CharLowCase = new StringBuffer();
		V_CharUpCase = new StringBuffer();
		V_Char = new StringBuffer();
		V_CharLow = new StringBuffer();
		// 添加数字
		for (int i = 0; i < 10; i++) {
			V_Number.append((char) (48 + i));
		}
		// 添加大写字母
		for (int j = 0; j < 26; j++) {
			V_CharUpCase.append((char) (65 + j));
		}
		// 添加小写字母
		for (int k = 0; k < 26; k++) {
			V_CharLowCase.append((char) (97 + k));
		}
		V_CharLow.append(V_Number);
		V_CharLow.append(V_CharLowCase);
		V_Char.append(V_Number);
		V_Char.append(V_CharUpCase);
		V_Char.append(V_CharLowCase);
	}
	/**
	 * 
	 * @param len 返回的字符串长度
	 * @return 返回大写字母加数字的字符串
	 */
    public static String getGuid(int len) {
        StringBuffer sb = new StringBuffer(len);
        for (int i = 0; i < len; i++) {
            int k;
            switch ((int) (Math.random() * 2D)) {
                case 0: // '\0'
                    k = (int) (Math.random() * 10D) + 48;
                    break;

                case 1: // '\001'
                    k = (int) (Math.random() * 26D) + 65;
                    break;

                default:
                    k = 95;
                    break;
            }
            sb.append((char) k);
        }
        String result=sb.toString(); 
        sb=null;
        return result;
    }

	public static void main(String[] args) {
		// TODO 自动生成方法存根
		/*StringBuffer Number = new StringBuffer();
		StringBuffer CharUpCase = new StringBuffer();
		StringBuffer CharLowCase = new StringBuffer();
		StringBuffer CharAll = new StringBuffer();
		StringBuffer ZiDingYi = new StringBuffer();
		Number.append(RandomPasswdCreater.V_Number.toString());
		CharUpCase.append(RandomPasswdCreater.V_CharUpCase.toString());
		CharLowCase.append(RandomPasswdCreater.V_CharLowCase.toString());
		CharAll.append(RandomPasswdCreater.V_Char.toString());
		ZiDingYi.append("[email=~!@#$%]~!@#$%[/email]^&*()囍`':?/|<>,.{}[]=-");
		// 输出16位随机数字
		System.out.println(RandomPasswdCreater.Produce(Number, 16).toString());
		// 输出16位随机英文大写
		System.out.println(RandomPasswdCreater.Produce(CharUpCase, 16)
				.toString());
		// 输出16位随机英文小写
		System.out.println(RandomPasswdCreater.Produce(CharLowCase, 16)
				.toString());
		// 输出16位随机数字加大小写英文
		System.out.println("CharAll:  "
				+ RandomPasswdCreater.Produce(CharAll, 16).toString());
		// 输出16位随机自定义字符
		System.out
				.println(RandomPasswdCreater.Produce(ZiDingYi, 16).toString());*/
		StringBuffer CharAll = new StringBuffer();
		CharAll.append(RandomPasswdCreater.V_Char.toString());
		System.out.println("CharAll:  "
				+ RandomPasswdCreater.Produce(CharAll, 8).toString());
	}
}
